package imp

import (
	sqlc "bgs/db/sqlc"
	activityGrpc "bgs/grpc/gen/service/activity"
	"bgs/grpc/gen/shared/message"
	activityService "bgs/service/activity"
	"context"
)

/****************************************************************************************/
/*                            activityOfStoreForC entity of conv                        */
/****************************************************************************************/


func transformListActivityItemOfU2CWithStatAndOwnerInfosForUserByStoreIDInStoreOwnRowEntityToPb(o sqlc.ListActivityItemOfU2CWithStatAndOwnerInfosForUserByStoreIDInStoreOwnRow) *activityGrpc.ActivityItemOfU2CWithStatAndOwnerInfoEntity {
	return &activityGrpc.ActivityItemOfU2CWithStatAndOwnerInfoEntity{
		// activity
		Id:            o.ID,
		BrandId:       o.BrandID,
		PubUserId:     o.PubUserID,
		Name:          o.Name,
		Type:          o.Type,
		Intro:         string(o.Intro),
		ProcessStatus: o.ProcessStatus,
		Published:     sqlc.GoNTimeToPBTimestamp(o.Published),
		Begined:       sqlc.GoTimeToPBTimestamp(o.Begined),
		Ended:         sqlc.GoTimeToPBTimestamp(o.Ended),
		TimeRanges:    transformTimeRangeEntityToPb(o.TimeRanges),
		SpokesmanId:   o.SpokesmanID.Int64,
		Owner:         o.Owner,
		OwnerName:     o.OwnerName,
		OwnerPic:      o.OwnerPic.String,

		// activity_stat
		OrderCount:     o.OrderCount,
		LatestAccessed: sqlc.GoTimeToPBTimestamp(o.LatestAccessed),
		Pv:             o.Pv,
		Uv:             o.Uv,
		Iv:             o.Iv,

		// permission
		// CanManage:          row.CanManage,
		// EndorseSpokesmanId: row.EndorseSpokesmanID,
		// OpenEndorseToUser:  row.OpenEndorseToUser,
		// HasNotEndorsed:     row.HasNotEndorsed,
	}
}

func mapListActivityItemOfU2CWithStatAndOwnerInfosForUserByStoreIDInStoreOwnRowEntityToPb(list []sqlc.ListActivityItemOfU2CWithStatAndOwnerInfosForUserByStoreIDInStoreOwnRow, f func(sqlc.ListActivityItemOfU2CWithStatAndOwnerInfosForUserByStoreIDInStoreOwnRow) *activityGrpc.ActivityItemOfU2CWithStatAndOwnerInfoEntity) []*activityGrpc.ActivityItemOfU2CWithStatAndOwnerInfoEntity {
	r := make([]*activityGrpc.ActivityItemOfU2CWithStatAndOwnerInfoEntity, len(list))
	for i, item := range list {
		r[i] = f(item)
	}
	return r
}

/****************************************************************************************/
/*                            activityOfStoreForC entity of query                       */
/****************************************************************************************/

// ListActivityItemOfU2CWithStatAndOwnerInfosForUserByStoreIDInStoreOwn C端用户进入门店首页查看门店自由活动列表及其统计、权限信息
func (s *ActivityAPIService) ListActivityItemOfU2CWithStatAndOwnerInfosForUserByStoreIDInStoreOwn(ctx context.Context, req *activityGrpc.ListActivityItemOfU2CWithStatAndOwnerInfosForUserByStoreIDInStoreOwnRequest) (res *activityGrpc.ListActivityItemOfU2CWithStatAndOwnerInfosForUserByStoreIDInStoreOwnResponse, err error) {
	slog := s.NewContextLogger(ctx)
	slog.Infof("activityImp->ListActivityItemOfU2CWithStatAndOwnerInfosForUserByStoreIDInStoreOwn:%v", req)
	res = &activityGrpc.ListActivityItemOfU2CWithStatAndOwnerInfosForUserByStoreIDInStoreOwnResponse{
		OpRet: &message.OperationResult{
			Success: true,
			Code:    0,
		},
	}
	q := s.dao.Q
	ts := sqlc.PBTimestampToGoTime(req.Ts)
	asFeatureTypes := activityService.ActivitiesAsFeature
	storeID := req.StoreId
	dateBegin := sqlc.PBTimestampToGoTime(req.DateBegin)
	dateEnd := sqlc.PBTimestampToGoTime(req.DateEnd)

	total, err := q.CountActivityItemOfU2CWithStatAndOwnerInfosForUserByStoreIDInStoreOwn(ctx, sqlc.CountActivityItemOfU2CWithStatAndOwnerInfosForUserByStoreIDInStoreOwnParams{
		AsFeatureTypes: asFeatureTypes,
		Created:        ts,
		StoreID:        sqlc.NInt64FromInt64(storeID),
		DateBegin:      dateBegin,
		DateEnd:        dateEnd,
	})
	if err != nil {
		res.OpRet = &message.OperationResult{
			Success: false,
			Code:    400,
			Msg:     err.Error(),
		}
		return
	}

	if total == 0 {
		// 不用继续调用listfunc
		return
	}

	rows, err := q.ListActivityItemOfU2CWithStatAndOwnerInfosForUserByStoreIDInStoreOwn(ctx, sqlc.ListActivityItemOfU2CWithStatAndOwnerInfosForUserByStoreIDInStoreOwnParams{
		Limit:          req.Limit,
		Offset:         req.Offset,
		AsFeatureTypes: asFeatureTypes,
		Created:        ts,
		StoreID:        sqlc.NInt64FromInt64(storeID),
		DateBegin:      dateBegin,
		DateEnd:        dateEnd,
	})
	if err != nil {
		res.OpRet = &message.OperationResult{
			Success: false,
			Code:    400,
			Msg:     err.Error(),
		}
		return
	}
	res.Total = total
	// 会有性能损耗,但是代码量精简了,暂时这个接口用,观察一下
	res.ActivityItemOfU2CWithStatAndOwnerInfos = mapListActivityItemOfU2CWithStatAndOwnerInfosForUserByStoreIDInStoreOwnRowEntityToPb(rows, transformListActivityItemOfU2CWithStatAndOwnerInfosForUserByStoreIDInStoreOwnRowEntityToPb)
	return
}
